<template>
  <el-menu :default-active="defaultActive" :router="router" v-bind="$attrs" class="el-menu-vertical-demo">
    <template v-for="(item, index) in data" :key="index">
      <el-menu-item :index="item.index" v-if="!item.children || !item.children.length">
        <component :is="`el-icon-${toLine(item.icon)}`" v-if="item.icon"></component>
        <span>{{ item.name }}</span>
      </el-menu-item>

      <el-sub-menu index="item.index" v-if="item.children && item.children.length">
        <template #title>
          <component :is="`el-icon-${toLine(item.icon)}`" v-if="item.icon"></component>
          <span>{{ item.name }}</span>
        </template>
        <el-menu-item v-for="(child, ind) in item.children" :key="ind">
          <component :is="`el-icon-${toLine(child.icon)}`" v-if="child.icon"></component>
          <span>{{ child.name }}</span>
        </el-menu-item>
      </el-sub-menu>
    </template>
  </el-menu>
</template>

<script setup lang="ts">
import { MenuItem } from './type'
import { toLine } from '../../utils/index'

let props = defineProps({
  data: {
    type: Array as PropType<MenuItem[]>,
    required: true
  },
  defaultActive: {
    type: String,
    default: ''
  },
  router: {
    type: Boolean,
    default: false
  }
})
  // console.log(props.data);

</script>

<style scoped>
svg {
  margin-right: 4px;
  width: 1em;
  height: 1em;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
}
</style>
